/**
 * @Title: CurrentUserMethodArgumentResolver.java
 * @Package com.lemon.conf
 * @Description: (一句话阐述):
 * @author Ran_cc
 * @date 2018年5月13日
 * @version V1.0
 */
package com.lemon.conf.MyInterceptor;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import com.lemon.model.cityRun.CrShopUser;

/**
 * @description:自定义解析器实现参数绑定
 * 							增加方法注入，将含有 @CurrentUser 注解的方法参数注入当前登录用户
 */
public class CurrentUserMethodArgumentResolver implements HandlerMethodArgumentResolver {
	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		return parameter.getParameterType().isAssignableFrom(CrShopUser.class)  //此处添加需要映射的实体类
				&& parameter.hasParameterAnnotation(CurrentUser.class);
	}

	@Override
	public Object resolveArgument(MethodParameter parameter , ModelAndViewContainer mavContainer ,
			NativeWebRequest webRequest , WebDataBinderFactory binderFactory) throws Exception {
		Object user = webRequest.getAttribute(CurrentUserConstants.CURRENT_USER , RequestAttributes.SCOPE_REQUEST);
		if (user != null) {
			return user;
		}
		throw new MissingServletRequestPartException(CurrentUserConstants.CURRENT_USER);
	}
}